package com.example.blog.controller;

import com.example.blog.dto.ArticleDTO;
import com.example.blog.pojo.Article;
import com.example.blog.pojo.PageBean;
import com.example.blog.pojo.Result;
import com.example.blog.service.ArticleService;
import com.example.blog.utils.ThreadLocalUtil;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin(origins = "http://localhost:5173")
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

//    获取文章详细信息
    @GetMapping("/detail")
    public Result<Article> getdetailsarticle(@RequestParam("id")Integer id){

        Article article = articleService.getArticle(id);
        return Result.success(article);

    }
//    获取文章列表
    @GetMapping("/list")
    public Result<PageBean<ArticleDTO>> getarticlelist(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) Integer status
    ){

        System.out.println("当前页: "+pageNum);
        System.out.println("页面大小: "+pageSize);
        System.out.println("分类: "+categoryId);
        System.out.println("状态: "+status);

        PageBean<ArticleDTO> pb =  articleService.getArticleList(pageNum,pageSize,categoryId,status);
        return Result.success(pb);

    }

//    用户空间的文章列表
    @GetMapping("/lists")
    public Result<List<ArticleDTO>> getuserarticlelist(@RequestParam("userid")Integer userid){

        List<ArticleDTO> userArticle = articleService.getUserArticle(userid,1);
        return Result.success(userArticle);

    }

//    帖子管理中的列表
    @GetMapping("/all")
    public Result<List<ArticleDTO>> getallarticle(){

        //        获取用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");

        List<ArticleDTO> list = articleService.getUserArticle(userid,null);

        return Result.success(list);

    }

//  新增文章
    @PostMapping("/add")
    public Result addarticle(@Validated @RequestBody Article article){

        //        获取用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");
        article.setUserid(userid);

        boolean s = articleService.addArticle(article);
        if(s)return Result.success();
        else return Result.error("发布失败!");


    }


//    用户修改文章标题,内容,类别
    @PatchMapping("/update")
    public Result updatearticle(@RequestParam("id")Integer id,@RequestParam("title")String title,@RequestParam("category")Integer category,@RequestParam("abstracts")String abstracts,@RequestParam("content")String content){


        //        获取当前用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");

        boolean s = articleService.updateArticle(userid,id,title,content,category,abstracts);
        if(s)return Result.success();
        else return Result.error("更新失败!");

    }

//    用户点赞
    @GetMapping("/like")
    public Result likearticle(@RequestParam("id")Integer articleid){
        //        获取当前用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");

        boolean s = articleService.likeArticle(articleid);
        if(s)return Result.success();
        else return Result.error("更新失败!");

    }

//    删除文章
    @DeleteMapping("/delete")
    public Result deletearticle(@RequestParam("id")Integer articleid){

        //        获取当前用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");

        boolean s = articleService.deleteArticle(articleid,userid);

        if(s)return Result.success();
        else return Result.error("删除失败!");

    }



}
